using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using Graffiti.Core;

namespace Graffiti.Redirect
{
    [Serializable]
    public class RedirectRecord
    {
        private string _regex;
        private string _target;
        private string _specialTarget;
        private string _redirectType;

        public string Regex
        {
            get { return _regex; }
            set { _regex = value; }
        }

        public string Target
        {
            get { return _target; }
            set { _target = value; }
        }

        public string SpecialTarget
        {
            get { return _specialTarget; }
            set { _specialTarget = value; }
        }

        public string RedirectType
        {
            get { return _redirectType; }
            set { _redirectType = value; }
        }

        public static List<RedirectRecord> DeSerialize(NameValueCollection nvc)
        {
            List<RedirectRecord> redirectRecords = new List<RedirectRecord>();
            if (nvc["regex"] != null)
            {
                string[] regexCollection = nvc["regex"].Split(',');
                string[] targetCollection = nvc["target"].Split(',');
                string[] sptargetCollection = nvc["sptarget"].Split(',');
                string[] redirectTypeCollection = nvc["redirectType"].Split(',');

                for (int i = 0; i < regexCollection.Length; i++)
                {
                    if (regexCollection[i] != string.Empty)
                    {
                        RedirectRecord rr = new RedirectRecord();
                        rr.Regex = regexCollection[i];
                        rr.Target = targetCollection[i];
                        rr.SpecialTarget = sptargetCollection[i];
                        rr.RedirectType = redirectTypeCollection[i];

                        redirectRecords.Add(rr);
                    }
                }
            }


            return redirectRecords;
        }

        internal string GetTarget(Match m)
        {
            if(Target != string.Empty)
            {
                string url = _target;

                for (int i = 0; i < m.Groups.Count; i++)
                {
                    url = url.Replace("{" + i.ToString() + "}", m.Groups[i].Value);
                }

                return url;
            }

            if(SpecialTarget == "Post ID")
            {
                int i = Convert.ToInt32(m.Groups[m.Groups.Count - 1].Value);
                Post post = new Post(i);
                return post.Url;
            }


            if (SpecialTarget == "Post Name")
            {
                string name = m.Groups[m.Groups.Count - 1].Value;
                int i = Post.GetPostIdByName(name);
                Post post = new Post(i);
                return post.Url;
            }

            return "";
        }
    }
}
